package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.SalesService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    SalesService salesService;

    /**
     * 销售机会分页查询
     * @param type
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/sales-list")
    @ResponseBody
    public Map<String,Object> salesList(String search,String type,Integer pageNum,Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }

        salesService.selectAllSales(search,type,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 销售机会客户列表
     * @param khsearch
     * @return
     */
    @RequestMapping("/kh-list")
    @ResponseBody
    public Map<String,Object> selectKh(Integer pageNum,Integer pageSize,String khsearch,String empName){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        salesService.selectKh(khsearch,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 客户联系人列表
     * @param khName
     * @return
     */
    @RequestMapping("/lxr-list")
    @ResponseBody
    public List<Contacts> selectLxr(String khName){
        return salesService.selectLxr(khName);
    }

    /**
     * 新增机会和销售详情
     * @param details
     */
    @RequestMapping("/addSales")
    @ResponseBody
    public void addSales(String details){
        JSONObject obj = (JSONObject)JSONObject.parse(details);
        List detailss = obj.getObject("pur",List.class);
        Sales sales = obj.getObject("sales",Sales.class);
        System.out.println("销售详情"+sales);
        salesService.addSales(sales,detailss);
    }

    /**
     * 查询要修改的销售机会
     * @param salessId
     * @return
     */
    @RequestMapping("/selectSales")
    @ResponseBody
    public List<Sales> selectSales(Integer salessId){
        System.out.println(salesService.Selectsalest(salessId));
        return salesService.Selectsalest(salessId);
    }

    /**
     * 查询要修改的销售详情
     * @param salessId
     * @return
     */
    @RequestMapping("/selectSalesDetails")
    @ResponseBody
    public List<Salesdetails> selectSalesDetails(Integer salessId){
        return salesService.Salesxq(salessId);
    }

    /**
     * 查询签了合同的销售机会不能被删除
     * @param salesId
     * @return
     */
    @RequestMapping("/selectHt")
    @ResponseBody
    public Hetong selectHt(Integer salesId){
        return salesService.selectHt(salesId);
    }

    /**
     * 根据销售id删除销售机会
     * @param salessId
     */
    @RequestMapping("/delSales")
    @ResponseBody
    public void delSales(Integer salessId){
        salesService.delSales(salessId);
    }

    /**
     * 销售机会预期签单时间(统计图)
     * @param salesMan
     * @return
     */
    @RequestMapping("/selectsDate")
    @ResponseBody
    public List<SalesVo> selectsDate(String salesMan){
        return salesService.selectsDate(salesMan);
    }

    /**
     * 销售机会预期签单时间月份(统计图)
     * @param salesMan
     * @return
     */
    @RequestMapping("/selectsDateMonth")
    @ResponseBody
    public List<SalesVo> selectsDateMonth(String salesMan){
        return salesService.selectsDateMonth(salesMan);
    }


    /**
     * 查询客户的来源(统计图)
     * @param salesMan
     * @return
     */
    @RequestMapping("/selectLaiy")
    @ResponseBody
    public List<SalesVo> selectLaiy(String salesMan){
        return salesService.selectLaiy(salesMan);
    }

    /**
     * 查询机会的来源(统计图)
     * @param salesMan
     * @return
     */
    @RequestMapping("/selectPossibility")
    @ResponseBody
    public List<SalesVo> selectPossibility(String salesMan){
        return salesService.selectPossibility(salesMan);
    }

    /**
     * 销售阶段的阶段数量(漏斗)
     * @param salesMan
     * @return
     */
    @RequestMapping("/stageNum")
    @ResponseBody
    public List<SalesVo> stageNum(String salesMan){
        return salesService.stageNum(salesMan);
    }

    /**
     * 销售阶段金额(漏斗)
     * @param salesMan
     * @return
     */
    @RequestMapping("/stageMoney")
    @ResponseBody
    public List<SalesVo> stageMoney(String salesMan){
        return salesService.stageMoney(salesMan);
    }

    /**
     * 销售漏斗选择框
     * @return
     */
    @RequestMapping("/userList")
    @ResponseBody
    public List<User> selectUser(){
        return salesService.selectUser();
    }
}
